package com.meyou.javascript.rewriter;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URISyntaxException;

import org.mozilla.javascript.CompilerEnvirons;
import org.mozilla.javascript.Parser;
import org.mozilla.javascript.ast.AstRoot;
import org.mozilla.javascript.tools.ToolErrorReporter;

import junit.framework.TestCase;

public abstract class BaseTestCase extends TestCase {

	protected Parser buildParser()
	{
		ToolErrorReporter reporter = new ToolErrorReporter(true);
		CompilerEnvirons compilerEnv = new CompilerEnvirons();
        compilerEnv.setErrorReporter(reporter);
        Parser p = new Parser(compilerEnv);
        
        return p;
	}
	
	protected void verifyScript(File file, String source)
	throws URISyntaxException, IOException
	{
		verifyScript(file, source, 1);
	}
	
	protected void verifyScript(File file, String source, int idx)
		throws URISyntaxException, IOException
	{
		String sourceLocation = file.getParent();
		sourceLocation = sourceLocation.replace("bin/", "test/");
		
		String name = file.getName().replace(".js", "-" + idx + ".js");
		
        file = new File(sourceLocation, name);
		
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		baos.write(source.getBytes());
		
		baos.writeTo(new FileOutputStream(file));
		baos.flush();
		
		baos.close();
		
		Parser p = buildParser();
		
        int lineno = 1;
        
        AstRoot ast = p.parse(source, sourceLocation, lineno);
	}
}
